package rpc.gray;



import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import rpc.health.FailEvent;
import rpc.registry.RegistryEvent;
import rpc.util.Subscribe;
import rpc.util.ThreadMode;

import java.net.InetSocketAddress;
import java.util.Map;

public class GrayListener {
    @Autowired
    Map<InetSocketAddress, InetSocketAddress> grayNodes;



    @Subscribe(threadMode = ThreadMode.BACKGROUND)
    public void onMessageEvent(RegistryEvent event) {
        if (event.isGray){
            InetSocketAddress clientAddress = null;
            InetSocketAddress serverAddress = null;
            String[] clientAddressArray = event.clientAddress.split(":");
            if (clientAddressArray.length == 2) { // Should check IP and port
                String host = clientAddressArray[0];
                int port = Integer.parseInt(clientAddressArray[1]);
                clientAddress = new InetSocketAddress(host, port);
            }

            String[] serverAddressArray = event.address.split(":");
            if (serverAddressArray.length == 2) { // Should check IP and port
                String host = serverAddressArray[0];
                int port = Integer.parseInt(serverAddressArray[1]);
                serverAddress = new InetSocketAddress(host, port);
            }
            grayNodes.put(clientAddress, serverAddress);
        }
    }
}
